﻿/// <reference path="jquery-1.4.4.js" />
/// <reference path="jquery-ui-1.8.6.custom.min.js" />
/// <reference path="jquery.blockUI.js" />

//弹出窗口
//dlg-窗口, url-目标地址,msg-遮罩信息,dlgOptions-对话框选项（详见JqueryUI Dialog）
function dialog(dlg, url, msg, dlgOptions) {
    var code = $.ajax({
        url: url,
        beforeSend: function () {
            dlg.dialog("close");
            (msg == undefined || msg == null) ? $.blockUI() : ChangeBlockMsg(msg); //遮罩
        },
        success: function (data) {
            dlg.html(data);
            ChangeBlockMsg(null);
            (dlgOptions == undefined || dlgOptions == null) ? dlg.dialog() : dlg.dialog(dlgOptions); //JqueryUI Dialog

            CancelButton($("input:button.cancel"), dlg);
        },

        complete: function () {
            if (code.status == 404 || code.status == 500) {
                var errorPanel = $("#errorPanel");
                $("#labErrorMsg").text("访问错误，请稍后再试或联系管理员");
                function errorCallback() {
                    $.unblockUI();
                };
                errorPanel.dialog({ position: "center", title: "错误", resizable: false, close: errorCallback });
                errorPanel.find("#btnErrorMsg").click(function () {
                    errorPanel.dialog("close");
                }).button();
            }

        }
    });

}

//初始化表单为AjaxForm
function initForm(dlg, form, callback, beforeSubmit, errorCallbackFun) {
    var submit = function (formData, jqForm, options) {
        var result = form.valid();
        var result2 = true;
        if (beforeSubmit != undefined && beforeSubmit != null) {
            result2 = beforeSubmit();
        }
        if (result && result2) {
            dlg.dialog("close");
            ChangeBlockMsg();
        }

        return result && result2;
    };

    var ajaxOption = {
        beforeSubmit: submit,
        success: function (data) {
            if (IsJson(data)) {
                var errorPanel = $("#errorPanel");
                $("#labErrorMsg").text(data.msg);
                dlg.parent().hide();
                function errorCallback() {
                    dlg.parent().show();
                    ReLoadTab();
                };
                if (errorCallbackFun != undefined && errorCallbackFun != null) {
                    errorPanel.dialog({ position: "center", height: "auto", width: "auto", title: "错误", resizable: false, close: errorCallbackFun });
                    errorPanel.find("#btnErrorMsg").click(function () {
                        errorPanel.dialog("close");
                        errorCallbackFun();
                    }).button();
                } else {
                    errorPanel.dialog({ position: "center", height: "auto", width: "auto", title: "错误", resizable: false, close: errorCallback });
                    errorPanel.find("#btnErrorMsg").click(function () {
                        errorPanel.dialog("close");
                    }).button();
                }

            } else {
                ChangeBlockMsg(null);
                dlg.html(data);
                dlg.dialog({ title: "信息", resizable: false, height: "auto", width: "auto", close: function () { $.unblockUI(); ReLoadTab(); } });
                CancelButton($("input:button.cancel"), dlg);
                if (callback != undefined && callback != null) {
                    callback(data);
                }
            }

            
        }
    };
    form.find("input:submit,input:button").button();
    form.ajaxForm(ajaxOption);
    form.find("input:checkbox").each(function () { $(this).rules('remove', 'required'); });
}

function ReLoadTab() {
    //test reload tab data
    if (typeof (tabID) != "undefined" && typeof (tabUrl) != "undefined") {
        $.get(tabUrl, function (data) {
            $("#" + tabID).html(data);
            InitDialogLink($("a.dialog"), $("#dialogPanel"));
        });
    }
    if (typeof (reflashTag) != "undefined" ) {
        window.location.reload();
    }
    //test
}

//初始化链接为弹出窗口
//items-链接,dlg-窗口
function InitDialogLink(items, dlg) {
    //    items.each(function () {
    //        var item = $(this);
    //        var link = item.attr("href");
    //        item.attr("href", "javascript:void(0);");
    //        item.click(function () {
    //            dialog(dlg, link, null, { position: "center",draggable:false,title:item.text(), resizable: false, close: function () { $.unblockUI(); } });
    //        });
    //    }).button();
    DialogLink(items, dlg, { position: "center", width: "auto", height: "auto", draggable: false, resizable: false, close: function () { $.unblockUI(); } });
}

//function DialogLink(items, dlg, options) {
//    items.each(function () {
//        var item = $(this);
//        var link = item.attr("href");
//        //验证是否有访问权限
//        $.ajax({
//            url: link,
//            type: "POST",
//            data: { perchk: true },
//            success: function (data) {
//                if (data.permission) {
//                    item.attr("href", "javascript:void(0);");
//                    options.title = item.text();
//                    item.click(function () {
//                        dialog(dlg, link, null, options);
//                    }).button();
//                } else {
//                    item.remove();
//                }
//            }
//        });

//    });
//}

function DialogLink(items, dlg, options) {

    items.each(function () {
        var item = $(this);
        var link = item.attr("href");
        item.attr("href", "javascript:void(0);")
        item.append($("<input type='hidden'></input").val(link));
        item.click(function () {
            options.title=$(this).text();
            dialog(dlg, link, null, options);
        });
    }).button();

    var index = 0;
    function Checkprev() {
        var item = $(items[index]);
        var link = item.find("input[type=hidden]").val();
        //验证是否有访问权限
        $.ajax({
            url: link,
            type: "POST",
            data: { perchk: true },
            success: function (data) {
                if (data.permission!=true) {
                    item.remove();
                }
                index++;
                if (index < items.length) {
                    Checkprev();
                }
            },
            error: function () {
                item.remove();
                index++;
                if (index < items.length) {
                    Checkprev();
                }
            }
        });
    }
    Checkprev();
    $(".button").button();
}

//判断数据是否Json
function IsJson(data) {
    var result;
    try {
        result = eval("(" + data + ")");
    } catch (ex) {
        result = data;
    }
    return result.error != undefined;
}

//把Table使用jqGrid显示，包括自适应宽度
function Grid(selector) {
    var resizeTag = null;
    function SetGrid() {
        var tab = $(".ui-tabs-panel").not(".ui-tabs-hide");
        if (tab.length > 0) {
            tableToGrid(selector, { height: "auto", width: tab.width(), multiselect: false });
        }
        else {
            tableToGrid(selector);
        }
    }
    function OnResize() {
        if (resizeTag != null) {
            clearTimeout(resizeTag);
        }
        resizeTag = setTimeout(function () {
            var tab = $(".ui-tabs-panel").not(".ui-tabs-hide");
            selector.setGridWidth(tab.width());
        }, 100);
    }
    SetGrid();
    $(window).resize(OnResize);
    selector.find("input[type=button],a").button();
}


function ChangeBlockMsg(msg) {
    $.blockUI({ message: msg, fadeOut: 0, fadeIn: 0 });
}

function CancelButton(calcel, dlg) {
    if (calcel != null) {
        calcel.click(function () {
            dlg.dialog("close");
            $.unblockUI();
        }).button();
    }
}

function GenreDestopIcon() {
    var app = $("#app");
    var option = {};
    $.ajax({
        url: '/Json/GetDesktop',
        type: "POST",
        success: function (data) {
            $.each(data, function (index, data) {
                app.append($('<div>' +
                '<a id="desktop_' + index + '" class="dock-item" href="#">' +
                    '<img src="' + data.Icon + '" alt="' + data.LinkName + '" /><br />' +
                    '<span>' + data.LinkName + '</span> </a>' +
            '</div>'));
                option[index] = {
                    width: data.Width,
                    height: data.Height,
                    fullscreen: data.Fullscreen,
                    title: data.LinkName,
                    url: data.Url,
                    callee: this,
                    center: true,
                    minable: data.Minable,
                    isIframe: true,
                    uniqueTag: data.LinkID
                };
                $('#desktop_' + index).click(function () {
                    $("#container").win(option[$(this).attr("id").replace("desktop_", "")]);
                });
            });
            $("#container").windowsetup();
        }
    });
}